<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 17:14:35 apple $
 行政区域操作相关函数
*/
namespace core;
defined('BY_JYA') or exit('error');
class area{
	//省市区列表
	public function get_pcd_list(){
		$pcd_list=pdo_getall('core_area',array('type'=>1),array('code','title'));
		foreach($pcd_list as &$item){
			$item['city']=pdo_getall('core_area',array('parent_code'=>$item['code'],'type'=>2),array('code','title'));
			foreach($item['city'] as &$d){
				$d['district']=pdo_getall('core_area',array('parent_code'=>$d['code'],'type'=>3),array('code','title'));
			}
		}
		return $pcd_list;
	}
	//省市区列表(名称为下标)
	public function get_pcd_titlelist($keyword=''){
		if(!empty($keyword)){
			$params['title like']='%'.$keyword.'%';
			$params['type <']=4;
			
		}else{
			$params['type']=1;
			
		}
		$list=pdo_getall('core_area',$params);
		$newlist=array();	
		foreach($list as &$item){
			$address=explode(' ',$item['address']);
			if($item['type']==1){
				$newlist[$item['title']]=pdo_getall('core_area',array('parent_code'=>$item['code']),array('code','title'),'title');
				foreach($newlist[$item['title']] as $k=>&$d){
					$newlist[$item['title']][$k]=pdo_getall('core_area',array('parent_code'=>$d['code']),array('code','title'));
				}
			}
			if(!empty($keyword)){
				if($item['type']==1){
					$newlist[$address[0]]=pdo_getall('core_area',array('parent_code'=>$item['code']),array('code','title'),'title');
				}
				if($item['type']==2){
					$newlist[$address[0]]=$newlist[$address[0]]?$newlist[$address[0]]:array();
					$newlist[$address[0]][$address[1]]=pdo_getall('core_area',array('parent_code'=>$item['code']),array('code','title'),'title');
				}
				if($item['type']==3){
					$ds=pdo_get('core_area',array('code'=>$item['code']),array('code','title'));
					$newlist[$address[0]]=$newlist[$address[0]]?$newlist[$address[0]]:array();
					$newlist[$address[0]][$address[1]] = $newlist[$address[0]][$address[1]]?$newlist[$address[0]][$address[1]]:array();
					$newlist[$address[0]][$address[1]][] = $ds;
				}
			}
		}
		return $newlist;
	}

	//获取pcd数组
	public function get_pcd_array($code=''){
		$code1=pdo_get('core_area',array('code'=>$code),array('code','parent_code','title'));
		if($code1['parent_code']){
			$code2=pdo_get('core_area',array('code'=>$code1['parent_code']),array('code','parent_code','title'));
			if($code2['parent_code']){
				$code3=pdo_get('core_area',array('code'=>$code2['parent_code']),array('code','parent_code','title'));
				if($code3['parent_code']){
					$code4=pdo_get('core_area',array('code'=>$code3['parent_code']),array('code','parent_code','title'));
				}
			}
		}
		if($code4){
			$pcd=array(
				'province'=>$code4['title'],
				'city'=>$code3['title'],
				'district'=>$code2['title'],
				'street'=>$code1['title'],
			);	
		}elseif($code3){
			$pcd=array(
				'province'=>$code3['title'],
				'city'=>$code2['title'],
				'district'=>$code1['title'],
			);
		}elseif($code2){
			$pcd=array(
				'province'=>$code2['title'],
				'city'=>$code1['title'],
			);
		}
        $pcd=$pcd?:array('city'=>'');
		return $pcd;
	}
	//省市区列表
	public function get_citys($py='',$name=''){
		$params=array();
		if($py){
			$params['py']=$py;
		}
		if($name){
			$params['name like']='%'.$name.'%';
		}
		$citys=pdo_getall('core_city',$params);
		return $citys;
		foreach($citys as $item){
			if(!$item['province']){
				$parent_code=pdo_getcolumn('core_area',array('title'=>$item['name'],'type'=>2),'parent_code');
				$item['province']=pdo_getcolumn('core_area',array('code'=>$parent_code,'type'=>1),'title');
				pdo_update('core_city',array('province'=>$item['province']),array('id'=>$item['id']));
			}
		}
		return $citys;
	}
}